using System;
using System.Collections.Generic;
using Mbs.Collections;

namespace Mbs.Configuration
{
    [Serializable]
    public abstract class Node : INode
    {
        protected String internalName;
        protected String internalValue;
        private PropertySet attributes;
        private List<INode> children = new List<INode>();

        protected Node()
        {
            attributes = new PropertySet();
        }

        protected Node(StringComparer comparer)
        {
            attributes = new PropertySet(comparer);
        }

        public virtual String Name
        {
            get { return internalName; }
        }

        public virtual String Value
        {
            get { return internalValue; }
        }

        public virtual IList<INode> Nodes
        {
            get { return children; }

        }

        public virtual IPropertySet Attributes
        {
            get { return attributes; }

        }

        public virtual object GetValue(Type type, object defaultValue)
        {
            try
            {
                return Convert.ChangeType(Value, type);
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }
    }
}
